﻿<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Bezdelnick
 * Date: 12.04.13
 * Time: 15:13
 * To change this template use File | Settings | File Templates.
 */
require_once("Config.php");
require_once("common/Lang.php");
require_once("persistence/Film.php");
require_once("dao/FilmDao.php");

$filmDao = new FilmDao();
$films = $filmDao->load();

if (isset($_POST['action']) && $_POST['action'] == "add") {
    if (
        isset($_POST['add_film_name']) &&
        isset($_POST['add_film_description']) &&
        isset($_POST['add_film_year'])
    ) {
        $createdFilm = new Film();
        $createdFilm->setName($_POST['add_film_name']);
        $createdFilm->setDescription($_POST['add_film_description']);
        $createdFilm->setYear($_POST['add_film_year']);
        $filmDao->insertAndRelative(
            $createdFilm,
            $_POST['film_actors'],
            $_POST['film_genres'],
            $_POST['film_countries']
        );
    }
} else if (isset($_POST['action']) && $_POST['action'] == "edit") {
    if (isset($_POST['edit_film_id']) && isset($_POST['edit_film_first_name']) && isset($_POST['edit_film_last_name'])) {
        $editedFilm = new Film();
        $editedFilm->setId($_POST['edit_film_id']);
        $editedFilm->setName($_POST['edit_film_name']);
        $editedFilm->setDescription($_POST['edit_film_description']);
        $editedFilm->setYear($_POST['edit_film_year']);
        $filmDao->update($editedFilm);
    }
} else if (isset($_POST['action']) && $_POST['action'] == "remove") {
    if (isset($_POST['remove_film_id'])) {
        $filmDao->delete($_POST['remove_film_id']);
    }
}
?>

<!-- Content table -->
<table class="content-table" border="0">
    <thead style="background-color: #ffd700;">
    <tr>
        <td style="width: 10%;">ID</td>
        <td style="width: 25%;">Name</td>
        <td style="width: 30%;">Description</td>
        <td style="width: 10%;">Year</td>
        <td style="width: 25%;">Operations</td>
    </tr>
    </thead>
    <tbody>
    <? foreach ($films as $film) { ?>
    <tr style="background-color: #efefef;">
        <td><?= $film->getId() ?></td>
        <td><?= $film->getName() ?></td>
        <td><?= $film->getDescription() ?></td>
        <td><?= $film->getYear() ?></td>
        <td style="text-align: center;">
            <div class="jquery-button edit-film-link"
                 itemId="<?= $film->getId() ?>"><?= Lang::EDIT ?>
            </div>
            <div class="jquery-button remove-film-link"
                 itemId="<?= $film->getId() ?>"><?= Lang::REMOVE ?>
            </div>
        </td>
    </tr>
        <? } ?>
    </tbody>
</table>